package com.example.apidocserver.domain.user.event.impl;

import com.example.apidocserver.domain.user.entity.UsrUserAuths;
import com.example.apidocserver.domain.user.entity.UsrUsers;
import com.example.apidocserver.domain.user.event.UserAuthenticationEventService;
import com.example.apidocserver.domain.user.event.UserInfoEventService;
import com.example.apidocserver.domain.user.service.UsrUserAuthsService;
import com.example.apidocserver.domain.user.service.UsrUsersService;
import com.example.apidocserver.infrastructure.config.security.UserDetailsImpl;
import com.example.apidocserver.infrastructure.constants.SystemConstants;
import com.example.apidocserver.infrastructure.constants.UserConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户认证授权服务
 *
 * @author xyqlg
 */
@Service
@Slf4j
public class UserInfoEventServiceImpl implements UserInfoEventService {
    @Resource
    private UsrUsersService usrUsersService;

    private final Map<Integer, String> idNameMap = new HashMap<>(8);

    @Override
    public String getNickNameById(Integer usrId) {
        if (Objects.isNull(usrId)) {
            return "null";
        }

        if (idNameMap.containsKey(usrId)) {
            return idNameMap.get(usrId);
        }

        UsrUsers usrUsers = usrUsersService.queryById(Long.valueOf(usrId));
        if (Objects.isNull(usrUsers)) {
            return "null";
        }
        idNameMap.put(usrId, usrUsers.getNickName());
        return usrUsers.getNickName();
    }
}
